import QQMapWX from "@/static/common/js/qqmap-wx-jssdk.min.js";
import Store from "@/store";
// import weappJwt from "@/until/weapp-jwt.js";
export function formatDate(format = "YYYY-MM-DD", time) {
    const date = time ? new Date(time) : new Date();
    const flags = {
        YYYY: date.getFullYear,
        MM() {
            return date.getMonth() + 1;
        },
        DD: date.getDate,
        hh: date.getHours,
        mm: date.getMinutes,
        ss: date.getSeconds,
        week() {
            const week = ["日", "一", "二", "三", "四", "五", "六"];
            return week[date.getDay()];
        }
    };
    return format.replace(/Y{4}|M{2}|D{2}|h{2}|m{2}|s{2}|week/g, key => {
        const value = flags[key].call(date);
        return typeof value !== "number" || value > 9 ? value : "0" + value;
    });
}
//根据经纬度获取地址
export function getAddressByLocation(params) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(reject, 10000);
        const qqMap = new QQMapWX({ key: Store.state.vuex_wechatInfo.mapKey });
        qqMap.reverseGeocoder({
            ...params,
            success() {
                resolve(arguments);
            },
            fail(e) {
                reject(e);
            },
            complete() {
                clearTimeout(timer);
            }
        });
    });
}
/**
 * @method getChildInfo 获取孩子信息
 * @param {*} vm Vue实例
 * @returns {Promise}
 */
export function getChildInfo(vm) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(reject, 10000);
        if (Store.state.vuex_childInfo) {
            resolve(Store.state.vuex_childInfo);
        } else {
            vm.$u.api.WXCHATAPI.getCheckChildren()
                .then(res => {
                    if (res.data) {
                        Store.commit("$uStore", {
                            name: "vuex_childInfo",
                            value: res.data
                        });
                        resolve(res.data);
                    } else {
                        reject();
                    }
                })
                .catch(reject)
                .finally(() => clearTimeout(timer));
        }
    });
}
/**
 * @method getUserInfo 获取用户信息
 * @param {*} vm Vue实例
 * @returns {Promise}
 */
export function getUserInfo(vm) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(reject, 10000);
        if (Store.state.vuex_parentInfo) {
            resolve(Store.state.vuex_parentInfo);
        } else {
            vm.$u.api.WXCHATAPI.getUserInfo()
                .then(res => {
                    if (res.data) {
                        Store.commit("$uStore", {
                            name: "vuex_parentInfo",
                            value: res.data
                        });
                        resolve(res.data);
                    } else {
                        reject();
                    }
                })
                .catch(reject)
                .finally(() => clearTimeout(timer));
        }
    });
}

// 阿拉伯数字转中文数字
export function numberToChinese(num) {
    if (!/^\d*(\.\d*)?$/.test(num)) {
        alert("Number is wrong!");
        return "Number is wrong!";
    }
    var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
    var BB = new Array("", "十", "百", "千", "万", "亿", "点", "");
    var a = ("" + num).replace(/(^0*)/g, "").split("."),
        k = 0,
        re = "";
    for (var i = a[0].length - 1; i >= 0; i--) {
        switch (k) {
            case 0:
                re = BB[7] + re;
                break;
            case 4:
                if (!new RegExp("0{4}\\d{" + (a[0].length - i - 1) + "}$").test(a[0]))
                    re = BB[4] + re;
                break;
            case 8:
                re = BB[5] + re;
                BB[7] = BB[5];
                k = 0;
                break;
        }
        if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) re = AA[0] + re;
        if (a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k % 4] + re;
        k++;
    }
    if (a.length > 1) //加上小数部分(如果有小数部分) 
    {
        re += BB[6];
        for (var i = 0; i < a[1].length; i++) re += AA[a[1].charAt(i)];
    }
    // 去掉十几前面一
    re = re.replace(/^一十/, '十');
    return re;
}